package com.tuanzi.loan.workflow.listener;

import java.util.ArrayList;
import java.util.List;

import org.activiti.engine.delegate.event.ActivitiEvent;
import org.activiti.engine.delegate.event.ActivitiEventListener;
import org.springframework.stereotype.Component;

/**
 * @author Ray
 * @date 2018/01/12
 */
@Component
public class BussinessEventListener implements ActivitiEventListener {

	/** 工作流监听队列 */
	private List<WorkflowListener> workflowListeners = null;

	@Override
	public void onEvent(ActivitiEvent event) {
		this.execute(event);
	}

	private void execute(ActivitiEvent event) {
		if (null != workflowListeners && !workflowListeners.isEmpty()) {
			for (WorkflowListener workflowListener : workflowListeners) {
				workflowListener.onEvent(event);
			}
		}
	}

	@Override
	public boolean isFailOnException() {
		return false;
	}

	public List<WorkflowListener> getWorkflowListener() {
		return workflowListeners;
	}

	/**
	 * 添加监听器
	 * 
	 * @param listener
	 *            工作流监听器
	 */
	public void addWorkflowListener(WorkflowListener listener) {
		if (null == workflowListeners) {
			workflowListeners = new ArrayList<>();
		}
		workflowListeners.add(listener);
	}
}
